<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class shop_mod extends module
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        $this->spid = @$_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function get_one($id)
    {
        $row = $this->db->get_one("select * from " . table('shop') . " where id = " . $id);
        if ($row['id']) {
            $row = $this->shop_row($row);
        } else {
            error_404('店铺不存在');
        }
        return $row;
    }
    private function shop_row($row)
    {
        if ($row['prov']) {
            $addr[] = $row['prov'];
            $addr[] = $row['city'];
            $addr[] = $row['dist'];
            $addr_row = $this->module('address')->get_cn($addr);
            $row['prov_cn'] = @$addr_row[$row['prov']]['name'];
            $row['city_cn'] = @$addr_row[$row['city']]['name'];
            $row['dist_cn'] = @$addr_row[$row['dist']]['name'];
        }
        if ($this->uid) {
            $crow = $this->db->get_one("select * from " . table('collect_shop') . " where uid = " . $this->uid . " and spid = " . $row['id']);
            if ($crow['id']) {
                $row['collect_status'] = 1;
            } else {
                $row['collect_status'] = 0;
            }
        } else {
            $row['collect_status'] = 0;
        }
        $score_row = $this->db->get_one("select * from " . table('shop_score') . " where shop_id = " . $row['id']);
        if ($score_row) {
            $row['score'] = array('goods' => isset($score_row['goods']) ? $score_row['goods'] : '5.00', 'service' => isset($score_row['service']) ? $score_row['goods'] : '5.00', 'logistics' => isset($score_row['logistics']) ? $score_row['goods'] : '5.00');
        }
        return $row;
    }
    function get_one_base()
    {
        return $this->db->get_one("select * from " . table('shop') . " where id = " . $this->spid);
    }
    function get_shop($shop_id)
    {
        if (is_array($shop_id)) {
            $spid_in = implode(',', $shop_id);
            $query = $this->db->get_all("select id,name from " . table('shop') . " where id in (" . $spid_in . ")");
            if ($query) {
                foreach ($query as $v) {
                    $result[] = $v;
                }
                return $result;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    public function design($shop_id)
    {
        $result = $this->db->get_one("select * from " . table('shop_design') . " where shop_id = " . $shop_id);
        return $result;
    }
    function GetDistance($lat1, $lng1, $lat2, $lng2)
    {
        $PI = 3.1415926535898;
        $radLat1 = $lat1 * ($PI / 180);
        $radLat2 = $lat2 * ($PI / 180);
        $a = $radLat1 - $radLat2;
        $b = $lng1 * ($PI / 180) - $lng2 * ($PI / 180);
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * 6378.137;
        $s = round($s * 10000) / 10000;
        return $s;
    }
    function returnSquarePoint($lng, $lat, $distance = 5)
    {
        $dlng = 2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
        $dlng = rad2deg($dlng);
        $dlat = $distance / 6371;
        $dlat = rad2deg($dlat);
        return array('left-top' => array('lat' => $lat + $dlat, 'lng' => $lng - $dlng), 'right-top' => array('lat' => $lat + $dlat, 'lng' => $lng + $dlng), 'left-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng - $dlng), 'right-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng + $dlng));
    }
    public function get_all($arr)
    {
        $page = isset($arr['page']) ? $arr['page'] : 1;
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : 20;
        $page_type = isset($arr['page_type']) ? $arr['page_type'] : 1;
        $wsql = '';
        if (!empty($warr['cid'])) {
            $wsql .= ' cid = ' . $warr['cid'] . ' and ';
        }
        $lng = isset($arr['lng']) ? $arr['lng'] : false;
        $lat = isset($arr['lat']) ? $arr['lat'] : false;
        if ($lng == '' && $lat == '') {
            $overtime = time() + 3600 * 24 * 7;
            $lng = @$_COOKIE["sp_lng"];
            $lat = @$_COOKIE["sp_lat"];
        } else {
            setcookie("sp_lng", $lng, $overtime);
            setcookie("sp_lat", $lat, $overtime);
        }
        if ($lng && $lat) {
            $distribution_scope = cfg('shop_distribution_scope');
            $squares = $this->returnSquarePoint($lng, $lat, $distribution_scope);
            $lat_right_btm = $squares['right-bottom']['lat'];
            $lat_left_top = $squares['left-top']['lat'];
            $lng_left_top = $squares['left-top']['lng'];
            $lng_right_btm = $squares['right-bottom']['lng'];
            $wsql .= " lat <> 0 and lat > " . $lat_right_btm . " and lat < " . $lat_left_top . " and lng > " . $lng_left_top . " and lng < " . $lng_right_btm . " ";
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $sql = "select * from " . table('shop') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . ", " . $page_size;
        $query = $this->db->get_all($sql);
        if ($query) {
            foreach ($query as $v) {
                if ($lng && $lat) {
                    $juli = $this->getDistance($lat, $lng, $v['lat'], $v['lng'], 1);
                    $v['juli'] = round($juli, 2);
                }
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('shop') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total'], 'page_type' => $page_type));
            }
            return $data;
        }
    }
    function get_px()
    {
        $page = isset($arr['page']) ? $arr['page'] : 1;
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : 20;
        $name = isset($arr['name']) ? $arr['name'] : 'px_recommend';
        $wsql = $name . ' = 1 and ';
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $sql = "select * from " . table('shop') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . ", " . $page_size;
        $query = $this->db->get_all($sql);
        if ($query) {
            foreach ($query as $v) {
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('shop') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total'], 'page_type' => $page_type));
            }
            return $data;
        }
    }
    function type($tid = null)
    {
        $arr = array('1' => '个人', '2' => '企业', '3' => '自营');
        if (isset($arr[$tid])) {
            return $arr[$tid];
        } else {
            return $arr;
        }
    }
    public function collect($arr)
    {
        $page = isset($arr['page']) ? $arr['page'] : 1;
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : 20;
        $page_type = isset($arr['page_type']) ? $arr['page_type'] : 1;
        $cquery = $this->db->get_all("select * from " . table('collect_shop') . " where uid = " . $this->uid . " limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($cquery) {
            foreach ($cquery as $k => $v) {
                $sp_arr[] = $v['spid'];
            }
            $spin = implode(',', $sp_arr);
            $spall = $this->db->get_all("select id,name,logo,status,collect from " . table("shop") . " where id in(" . $spin . ") and status = 1");
            if ($spall) {
                foreach ($spall as $k => $v) {
                    $gall = $this->db->get_all("select id,shop_id,title,images,price,volume,comment,status from " . table("goods") . " where shop_id = " . $v['id'] . " and status = 1 limit 5");
                    $data['sp_row'] = $v;
                    $data['gs_all'] = $gall;
                    $result['list'][] = $data;
                }
            }
            $total = $this->db->get_one("select count(id) as total from " . table('collect_shop') . " where uid = " . $this->uid);
            $result['total'] = $total;
            if ($page_size < $total['total']) {
                $result['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total'], 'page_type' => $page_type));
            }
            return $result;
        }
    }
}